<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0,viewport-fit=cover"><title>Go的工程管理 | 夜悊的技术小宅</title><meta name="author" content="夜悊"><meta name="copyright" content="夜悊"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="前言Go的工程管理。  一、问题 不知道如何同时编译并运行多个Go文件 因为Go的语法规范，模块、目录、文件、包、导入包和导入函数不知道怎样命名和书写 因为Go的改进，不用传统配置GOPATH方式而是使用Go mod进行工程管理，但不知道怎样使用Go mod 目前关于Go工程管理的网上资料较少，使用GOPATH方式的多，总结Go mod方式的少   二、测试环境 Windows   注意：Wind">
<meta property="og:type" content="article">
<meta property="og:title" content="Go的工程管理">
<meta property="og:url" content="http://example.com/2023/04/14/Go%E7%9A%84%E5%B7%A5%E7%A8%8B%E7%AE%A1%E7%90%86/index.html">
<meta property="og:site_name" content="夜悊的技术小宅">
<meta property="og:description" content="前言Go的工程管理。  一、问题 不知道如何同时编译并运行多个Go文件 因为Go的语法规范，模块、目录、文件、包、导入包和导入函数不知道怎样命名和书写 因为Go的改进，不用传统配置GOPATH方式而是使用Go mod进行工程管理，但不知道怎样使用Go mod 目前关于Go工程管理的网上资料较少，使用GOPATH方式的多，总结Go mod方式的少   二、测试环境 Windows   注意：Wind">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://example.com/img/cover16.png">
<meta property="article:published_time" content="2023-04-14T08:52:49.000Z">
<meta property="article:modified_time" content="2023-04-14T09:58:32.357Z">
<meta property="article:author" content="夜悊">
<meta property="article:tag" content="Go">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://example.com/img/cover16.png"><link rel="shortcut icon" href="/img/favicon2.png"><link rel="canonical" href="http://example.com/2023/04/14/Go%E7%9A%84%E5%B7%A5%E7%A8%8B%E7%AE%A1%E7%90%86/index.html"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css?v=4.13.0"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6.5.1/css/all.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui@5.0.33/dist/fancybox/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = {
  root: '/',
  algolia: undefined,
  localSearch: {"path":"/search.xml","preload":true,"top_n_per_article":1,"unescape":false,"languages":{"hits_empty":"找不到您查询的内容：${query}","hits_stats":"共找到 ${hits} 篇文章"}},
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"簡"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlight.js","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  dateSuffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: {"limitCount":50,"languages":{"author":"作者: 夜悊","link":"链接: ","source":"来源: 夜悊的技术小宅","info":"著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。"}},
  lightbox: 'fancybox',
  Snackbar: undefined,
  infinitegrid: {
    js: 'https://cdn.jsdelivr.net/npm/@egjs/infinitegrid@4.11.1/dist/infinitegrid.min.js',
    buttonText: '加载更多'
  },
  isPhotoFigcaption: false,
  islazyload: true,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: false,
  },
  autoDarkmode: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'Go的工程管理',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2023-04-14 17:58:32'
}</script><script>(win=>{
      win.saveToLocal = {
        set: (key, value, ttl) => {
          if (ttl === 0) return
          const now = Date.now()
          const expiry = now + ttl * 86400000
          const item = {
            value,
            expiry
          }
          localStorage.setItem(key, JSON.stringify(item))
        },
      
        get: key => {
          const itemStr = localStorage.getItem(key)
      
          if (!itemStr) {
            return undefined
          }
          const item = JSON.parse(itemStr)
          const now = Date.now()
      
          if (now > item.expiry) {
            localStorage.removeItem(key)
            return undefined
          }
          return item.value
        }
      }
    
      win.getScript = (url, attr = {}) => new Promise((resolve, reject) => {
        const script = document.createElement('script')
        script.src = url
        script.async = true
        script.onerror = reject
        script.onload = script.onreadystatechange = function() {
          const loadState = this.readyState
          if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
          script.onload = script.onreadystatechange = null
          resolve()
        }

        Object.keys(attr).forEach(key => {
          script.setAttribute(key, attr[key])
        })

        document.head.appendChild(script)
      })
    
      win.getCSS = (url, id = false) => new Promise((resolve, reject) => {
        const link = document.createElement('link')
        link.rel = 'stylesheet'
        link.href = url
        if (id) link.id = id
        link.onerror = reject
        link.onload = link.onreadystatechange = function() {
          const loadState = this.readyState
          if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
          link.onload = link.onreadystatechange = null
          resolve()
        }
        document.head.appendChild(link)
      })
    
      win.activateDarkMode = () => {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = () => {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
        if (t === 'dark') activateDarkMode()
        else if (t === 'light') activateLightMode()
      
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
      const detectApple = () => {
        if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
          document.documentElement.classList.add('apple')
        }
      }
      detectApple()
    })(window)</script><link rel="stylesheet" href="/css/my_css.css"><meta name="generator" content="Hexo 7.3.0"></head><body><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src= "" data-lazy-src="/img/avatar.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">92</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">15</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">15</div></a></div><hr class="custom-hr"/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw fa fa-bookmark"></i><span> 笔记目录</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/categories/"><i class="fa-fw fa fa-folder-open"></i><span> 分类</span></a></li><li><a class="site-page child" href="/tags/"><i class="fa-fw fa fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/archives/"><i class="fa-fw fa fa-archive"></i><span> 归档</span></a></li></ul></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw fa fa-blog"></i><span> 其他博客</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" target="_blank" rel="noopener" href="https://blog.csdn.net/m0_62083249"><i class="fa-fw fa fa-link"></i><span> CSDN</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://www.zhihu.com/people/ye-zhe-ning"><i class="fa-fw fa fa-link"></i><span> 知乎</span></a></li></ul></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw fa fa-sitemap"></i><span> 代码仓库</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" target="_blank" rel="noopener" href="https://gitee.com/yezhening"><i class="fa-fw fa fa-link"></i><span> Gitee</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://github.com/yezhening"><i class="fa-fw fa fa-link"></i><span> Github</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/about_me/"><i class="fa-fw fas fa-user"></i><span> 关于我</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('/img/cover16.png')"><nav id="nav"><span id="blog-info"><a href="/" title="夜悊的技术小宅"><span class="site-name">夜悊的技术小宅</span></a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search" href="javascript:void(0);"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw fa fa-bookmark"></i><span> 笔记目录</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/categories/"><i class="fa-fw fa fa-folder-open"></i><span> 分类</span></a></li><li><a class="site-page child" href="/tags/"><i class="fa-fw fa fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/archives/"><i class="fa-fw fa fa-archive"></i><span> 归档</span></a></li></ul></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw fa fa-blog"></i><span> 其他博客</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" target="_blank" rel="noopener" href="https://blog.csdn.net/m0_62083249"><i class="fa-fw fa fa-link"></i><span> CSDN</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://www.zhihu.com/people/ye-zhe-ning"><i class="fa-fw fa fa-link"></i><span> 知乎</span></a></li></ul></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw fa fa-sitemap"></i><span> 代码仓库</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" target="_blank" rel="noopener" href="https://gitee.com/yezhening"><i class="fa-fw fa fa-link"></i><span> Gitee</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://github.com/yezhening"><i class="fa-fw fa fa-link"></i><span> Github</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/about_me/"><i class="fa-fw fas fa-user"></i><span> 关于我</span></a></div></div><div id="toggle-menu"><a class="site-page" href="javascript:void(0);"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">Go的工程管理</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2023-04-14T08:52:49.000Z" title="发表于 2023-04-14 16:52:49">2023-04-14</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2023-04-14T09:58:32.357Z" title="更新于 2023-04-14 17:58:32">2023-04-14</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/Go/">Go</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="Go的工程管理"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>Go的工程管理。</p>
<hr>
<h1 id="一、问题"><a href="#一、问题" class="headerlink" title="一、问题"></a>一、问题</h1><ul>
<li>不知道如何同时编译并运行多个Go文件</li>
<li>因为Go的语法规范，模块、目录、文件、包、导入包和导入函数不知道怎样命名和书写</li>
<li>因为Go的改进，不用传统配置GOPATH方式而是使用Go mod进行工程管理，但不知道怎样使用Go mod</li>
<li>目前关于Go工程管理的网上资料较少，使用GOPATH方式的多，总结Go mod方式的少</li>
</ul>
<hr>
<h1 id="二、测试环境"><a href="#二、测试环境" class="headerlink" title="二、测试环境"></a>二、测试环境</h1><ul>
<li>Windows</li>
</ul>
<blockquote>
<p>注意：Windows下的cmd和PowerShell命令行无法识别部分字符，所以不能执行部分Linux下可以执行的命令</p>
</blockquote>
<ul>
<li><p>VS Code</p>
</li>
<li><p>开启Go mod机制，命令行执行：</p>
</li>
</ul>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">go env <span class="literal">-w</span> GO111MODULE=on</span><br><span class="line"><span class="comment"># 开启Go mod机制</span></span><br><span class="line"></span><br><span class="line">go env</span><br><span class="line"><span class="comment"># 查看GO111MODULE变量是否开启</span></span><br></pre></td></tr></table></figure>

<hr>
<h1 id="三、同模块同目录同包"><a href="#三、同模块同目录同包" class="headerlink" title="三、同模块同目录同包"></a>三、同模块同目录同包</h1><h2 id="1-目录结构"><a href="#1-目录结构" class="headerlink" title="1.目录结构"></a>1.目录结构</h2><p>目录dir：</p>
<ul>
<li>文件a.go</li>
<li>文件b.go</li>
</ul>
<hr>
<h2 id="2-操作"><a href="#2-操作" class="headerlink" title="2.操作"></a>2.操作</h2><p>（1）编写文件b.go：</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> <span class="string">&quot;fmt&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 同包中，函数名首字母无需大写指定公有访问权限</span></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">test</span><span class="params">()</span></span> &#123;</span><br><span class="line">	fmt.Println(<span class="number">10</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>（2）编写文件a.go：</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line"><span class="comment">// 同包中，无需导入路径</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">	test()</span><br><span class="line">	<span class="comment">// 同包中，可直接调用相应函数</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>（3）命令行执行：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">cd</span> <span class="built_in">dir</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 方式一</span></span><br><span class="line">go run a.go b.go</span><br><span class="line"><span class="comment"># VS Code会在a.go报错找不到test()函数，但能够成功运行</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 方式二</span></span><br><span class="line">go run .</span><br><span class="line"><span class="comment"># 报错：go: go.mod file not found in current directory or any parent directory; see &#x27;go help modules&#x27;</span></span><br><span class="line"></span><br><span class="line">go mod init modu</span><br><span class="line"><span class="comment"># 通过创建mod文件，使得a.go能够找到依赖项b.go，所以能够运行</span></span><br><span class="line"><span class="comment"># VS Code不再报错，因为通过.mod文件让a.go找到其依赖项b.go</span></span><br><span class="line">go run .</span><br></pre></td></tr></table></figure>
<blockquote>
<p>在生成mod文件后，若在文件go.mod报错：gopls was not able to find modules in your workspace…，参见：<a target="_blank" rel="noopener" href="https://www.jianshu.com/p/fde21b198795">关于VS code报错 gopls requires a module at the root of your workspace - 简书 (jianshu.com)</a></p>
</blockquote>
<hr>
<h2 id="3-总结"><a href="#3-总结" class="headerlink" title="3.总结"></a>3.总结</h2><ul>
<li>同模块：使用“go mod init”初始化一个模块，<strong>模块名为modu</strong></li>
<li>同目录：多go文件在同一目录下，<strong>目录名为dir</strong></li>
<li>同包：多go文件在同包，<strong>包名为main</strong></li>
<li>文件名为a.go，b.go，<strong>没有特别命名</strong></li>
<li><strong>函数名为test，同包中，函数名首字母无需大写指定公有访问权限，无需导入路径，可直接调用相应函数</strong></li>
</ul>
<hr>
<h1 id="四、同模块不同目录不同包"><a href="#四、同模块不同目录不同包" class="headerlink" title="四、同模块不同目录不同包"></a>四、同模块不同目录不同包</h1><h2 id="1-目录结构-1"><a href="#1-目录结构-1" class="headerlink" title="1.目录结构"></a>1.目录结构</h2><p>目录dir：</p>
<ul>
<li>文件a.go</li>
<li>目录dir_b</li>
</ul>
<p>目录dir_b：</p>
<ul>
<li>文件b.go</li>
</ul>
<hr>
<h2 id="2-操作-1"><a href="#2-操作-1" class="headerlink" title="2.操作"></a>2.操作</h2><p>（1）编写文件b.go：</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> pkg_b</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> <span class="string">&quot;fmt&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 不同包中，函数名首字母需大写指定公有访问权限</span></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">Test</span><span class="params">()</span></span> &#123;</span><br><span class="line">	fmt.Println(<span class="number">10</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>（2）命令行执行：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">cd</span> <span class="built_in">dir</span></span><br><span class="line"></span><br><span class="line">go mod init modu</span><br></pre></td></tr></table></figure>

<p>（3）编写文件a.go：</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> <span class="string">&quot;modu/dir_b&quot;</span></span><br><span class="line"><span class="comment">// 格式：当前模块名/目录名</span></span><br><span class="line"><span class="comment">// 实际上，import是导入路径，而不是导入包名。所以是dir_b而不是pkg_b</span></span><br><span class="line"><span class="comment">// 奇怪的引用方式，应该把包名命名为目录名才能统一，不过一般同一目录下是同一个包，命名相同。这里为了突显导入格式，目录名和包名命名不同</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">	pkg_b.Test()</span><br><span class="line">	<span class="comment">// 使用函数需要用包名引用</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>（4）命令行执行：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">cd</span> <span class="built_in">dir</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 方式一</span></span><br><span class="line">go run .</span><br><span class="line"></span><br><span class="line"><span class="comment"># 方式二</span></span><br><span class="line">go run a.go</span><br></pre></td></tr></table></figure>
<hr>
<h2 id="3-总结-1"><a href="#3-总结-1" class="headerlink" title="3.总结"></a>3.总结</h2><ul>
<li>同模块：使用“go mod init”初始化一个模块，<strong>模块名为modu</strong></li>
<li>不同目录：多go文件在不同一目录下，<strong>目录名为dir和dir_b</strong></li>
<li>不同包：多go文件在不同包，<strong>包名为main和pkg_b</strong></li>
<li>文件名为a.go，b.go，<strong>没有特别命名</strong></li>
<li><strong>函数名为Test，不同包中，函数名首字母需大写指定公有访问权限</strong></li>
<li>导入依赖<strong>包-&gt;路径</strong>的形式：<strong>import 当前模块名&#x2F;目录名</strong></li>
<li>使用依赖函数的形式：<strong>包名.函数名()</strong></li>
</ul>
<hr>
<h1 id="五、不同模块不同目录不同包1"><a href="#五、不同模块不同目录不同包1" class="headerlink" title="五、不同模块不同目录不同包1"></a>五、不同模块不同目录不同包1</h1><h2 id="1-目录结构-2"><a href="#1-目录结构-2" class="headerlink" title="1.目录结构"></a>1.目录结构</h2><p>目录dir：</p>
<ul>
<li>文件a.go</li>
<li>目录dir_b</li>
</ul>
<p>目录dir_b：</p>
<ul>
<li>文件b.go</li>
</ul>
<hr>
<h2 id="2-操作："><a href="#2-操作：" class="headerlink" title="2.操作："></a>2.操作：</h2><p>（1）编写文件b.go：</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> pkg_b</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> <span class="string">&quot;fmt&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 不同包中，函数名首字母需大写指定公有访问权限</span></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">Test</span><span class="params">()</span></span> &#123;</span><br><span class="line">	fmt.Println(<span class="number">10</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>（2）命令行执行：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">cd</span> dir_b</span><br><span class="line"></span><br><span class="line">go mod init modu_b</span><br></pre></td></tr></table></figure>

<p>（3）编写a.go</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> <span class="string">&quot;modu_b&quot;</span></span><br><span class="line"><span class="comment">// 需要用的模块名</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">	pkg_b.Test()</span><br><span class="line">	<span class="comment">// 使用函数需要用包名引用</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>（4）命令行执行：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">cd</span> <span class="built_in">dir</span></span><br><span class="line"></span><br><span class="line">go mod init modu</span><br></pre></td></tr></table></figure>
<p>（5）修改dir下的go.mod文件：</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">module modu</span><br><span class="line"></span><br><span class="line"><span class="keyword">go</span> <span class="number">1.20</span></span><br><span class="line"></span><br><span class="line">require modu_b v0<span class="number">.0</span><span class="number">.0</span></span><br><span class="line"><span class="comment">// 需要用的模块名，版本</span></span><br><span class="line"></span><br><span class="line">replace modu_b =&gt; C:\Users\DSHH\Desktop\go_test\dir\dir_b</span><br><span class="line"><span class="comment">// 指定查找路径</span></span><br><span class="line"><span class="comment">// 导入该模块实际上是导入该路径</span></span><br></pre></td></tr></table></figure>
<h2 id="（5）命令行执行："><a href="#（5）命令行执行：" class="headerlink" title="（5）命令行执行："></a>（5）命令行执行：<br><figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">cd</span> <span class="built_in">dir</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 方式一</span></span><br><span class="line">go run .</span><br><span class="line"></span><br><span class="line"><span class="comment"># 方式二</span></span><br><span class="line">go run a.go</span><br></pre></td></tr></table></figure></h2><h2 id="3-总结-2"><a href="#3-总结-2" class="headerlink" title="3.总结"></a>3.总结</h2><ul>
<li>不同模块：使用“go mod init”初始化多个模块，<strong>模块名为modu和modu_b</strong></li>
<li>不同目录：多go文件在不同一目录下，<strong>目录名为dir和dir_b</strong></li>
<li>不同包：多go文件在不同包，<strong>包名为main和pkg_b</strong></li>
<li>文件名为a.go，b.go，<strong>没有特别命名</strong></li>
<li><strong>函数名为Test，不同包中，函数名首字母需大写指定公有访问权限，使用函数需要用包名引用</strong></li>
<li>导入依赖<strong>模块-&gt;路径</strong>的形式：<strong>import 模块名</strong></li>
<li>使用依赖函数的形式：<strong>包名.函数名()</strong></li>
</ul>
<hr>
<h1 id="六、不同模块不同目录不同包2"><a href="#六、不同模块不同目录不同包2" class="headerlink" title="六、不同模块不同目录不同包2"></a>六、不同模块不同目录不同包2</h1><h2 id="1-目录结构-3"><a href="#1-目录结构-3" class="headerlink" title="1.目录结构"></a>1.目录结构</h2><p>目录dir：</p>
<ul>
<li>目录dir_a</li>
<li>目录dir_b</li>
</ul>
<p>目录dir_a：</p>
<ul>
<li>文件a.go</li>
</ul>
<p>目录dir_b：</p>
<ul>
<li>文件b.go</li>
</ul>
<hr>
<h2 id="2-操作：-1"><a href="#2-操作：-1" class="headerlink" title="2.操作："></a>2.操作：</h2><p>（1）编写文件b.go：</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> pkg_b</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> <span class="string">&quot;fmt&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 不同包中，函数名首字母需大写指定公有访问权限</span></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">Test</span><span class="params">()</span></span> &#123;</span><br><span class="line">	fmt.Println(<span class="number">10</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>（2）命令行执行：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">cd</span> dir_b</span><br><span class="line"></span><br><span class="line">go mod init modu_b</span><br></pre></td></tr></table></figure>

<p>（3）编写a.go</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> <span class="string">&quot;modu_b&quot;</span></span><br><span class="line"><span class="comment">// 需要用的模块名</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">	pkg_b.Test()</span><br><span class="line">	<span class="comment">// 使用函数需要用包名引用</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>（4）命令行执行：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">cd</span> dir_a</span><br><span class="line"></span><br><span class="line">go mod init modu_a</span><br></pre></td></tr></table></figure>
<p>（5）修改dir_a下的go.mod文件：</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">module modu_a</span><br><span class="line"></span><br><span class="line"><span class="keyword">go</span> <span class="number">1.20</span></span><br><span class="line"></span><br><span class="line">require modu_b v0<span class="number">.0</span><span class="number">.0</span></span><br><span class="line"><span class="comment">// 需要用的模块名，版本</span></span><br><span class="line"></span><br><span class="line">replace modu_b =&gt; C:\Users\DSHH\Desktop\go_test\dir\dir_b</span><br><span class="line"><span class="comment">// 指定查找路径</span></span><br><span class="line"><span class="comment">// 导入该模块实际上是导入该路径</span></span><br></pre></td></tr></table></figure>
<h2 id="（5）命令行执行：-1"><a href="#（5）命令行执行：-1" class="headerlink" title="（5）命令行执行："></a>（5）命令行执行：<br><figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">cd</span> dir_a</span><br><span class="line"></span><br><span class="line"><span class="comment"># 方式一</span></span><br><span class="line">go run .</span><br><span class="line"></span><br><span class="line"><span class="comment"># 方式二</span></span><br><span class="line">go run a.go</span><br></pre></td></tr></table></figure></h2><h2 id="3-总结-3"><a href="#3-总结-3" class="headerlink" title="3.总结"></a>3.总结</h2><ul>
<li>不同模块：使用“go mod init”初始化多个模块，<strong>模块名为modu_a和modu_b</strong></li>
<li>不同目录：多go文件在不同一目录下，<strong>目录名为dir_a和dir_b</strong></li>
<li>不同包：多go文件在不同包，<strong>包名为main和pkg_b</strong></li>
<li>文件名为a.go，b.go，<strong>没有特别命名</strong></li>
<li><strong>函数名为Test，不同包中，函数名首字母需大写指定公有访问权限，使用函数需要用包名引用</strong></li>
<li>导入依赖<strong>模块-&gt;路径</strong>的形式：<strong>import 模块名</strong></li>
<li>使用依赖函数的形式：<strong>包名.函数名()</strong></li>
</ul>
<hr>
<h1 id="七、总结"><a href="#七、总结" class="headerlink" title="七、总结"></a>七、总结</h1><p>方式的选择：</p>
<ul>
<li><p>不使用旧的设置GOPATH变量方式，多文件编程不一定放在src目录下</p>
</li>
<li><p>原因：应该是引入新的Go mod模块机制替代了</p>
</li>
<li><p>建议使用新的方式，无需每次工程都要配置修改环境变量</p>
</li>
</ul>
<p>使用Go mod方式的建议：</p>
<ul>
<li>一个目录下的所有go文件都是同一个包，目录名和包名相同</li>
<li>运行同一个目录下的多个go文件（同个包），使用方式三</li>
<li>由第一点：“一个目录下的所有go文件都是同一个包”，方式四的使用并不常见，也容易混乱</li>
<li>运行不同目录下，目录有嵌套关系的多个go文件，使用方式五</li>
<li>运行不同目录下，目录是并列关系的多个go文件，使用方式六</li>
</ul>
<blockquote>
<p>实际上方式五和六的操作一样</p>
</blockquote>
<hr>
<h1 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h1><p>Go的工程管理。</p>
<hr>
<h1 id="参考资料"><a href="#参考资料" class="headerlink" title="参考资料"></a>参考资料</h1><ul>
<li><a target="_blank" rel="noopener" href="https://zhuanlan.zhihu.com/p/482014524">go mod使用 | 全网最详细 - 知乎 (zhihu.com)</a></li>
<li><a target="_blank" rel="noopener" href="https://segmentfault.com/q/1010000020590559">go mod怎么引用同一个项目下面的子目录 - SegmentFault 思否</a></li>
<li><a target="_blank" rel="noopener" href="https://blog.csdn.net/qq_43442524/article/details/105216529?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2~default~CTRLIST~Rate-1-105216529-blog-118687783.235%5Ev28%5Epc_relevant_recovery_v2&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2~default~CTRLIST~Rate-1-105216529-blog-118687783.235%5Ev28%5Epc_relevant_recovery_v2&utm_relevant_index=2">Golang1.14 go modules 怎么导入本地其它项目的包? 看完这篇文章, 终于搞懂了_golang 本地引用其它项目包_普通Gopher的博客-CSDN博客</a></li>
</ul>
<hr>
<h1 id="作者的话"><a href="#作者的话" class="headerlink" title="作者的话"></a>作者的话</h1><ul>
<li><strong>感谢参考资料的作者&#x2F;博主</strong></li>
<li>作者：夜悊</li>
<li>版权所有，转载请注明出处，谢谢~</li>
<li><strong>如果文章对你有帮助，请点个赞或加个粉丝吧，你的支持就是作者的动力~</strong></li>
<li>文章在描述时有疑惑的地方，请留言，定会一一耐心讨论、解答</li>
<li>文章在认识上有错误的地方, 敬请批评指正</li>
<li>望读者们都能有所收获</li>
</ul>
<hr>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta"><i class="fas fa-circle-user fa-fw"></i>文章作者: </span><span class="post-copyright-info"><a href="http://example.com">夜悊</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta"><i class="fas fa-square-arrow-up-right fa-fw"></i>文章链接: </span><span class="post-copyright-info"><a href="http://example.com/2023/04/14/Go%E7%9A%84%E5%B7%A5%E7%A8%8B%E7%AE%A1%E7%90%86/">http://example.com/2023/04/14/Go的工程管理/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta"><i class="fas fa-circle-exclamation fa-fw"></i>版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="http://example.com" target="_blank">夜悊的技术小宅</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/Go/">Go</a></div><div class="post_share"><div class="social-share" data-image="/img/cover16.png" data-sites="qq,wechat,weibo"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1.1.3/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1.1.3/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2023/02/13/%E5%9B%9E%E6%BA%AF%EF%BC%88backtrack%EF%BC%89%E6%8F%8F%E8%BF%B0/" title="回溯（backtrack）描述"><img class="cover" src= "" data-lazy-src="/img/cover9.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">回溯（backtrack）描述</div></div></a></div><div class="next-post pull-right"><a href="/2023/04/16/Go%E5%B9%B6%E5%8F%91%E7%BC%96%E7%A8%8B%E7%9A%84%E5%AD%A6%E4%B9%A0%E4%BB%A3%E7%A0%81%E7%A4%BA%E4%BE%8B%EF%BC%9A%E7%94%9F%E4%BA%A7%E8%80%85%E6%B6%88%E8%B4%B9%E8%80%85%E6%A8%A1%E5%9E%8B/" title="Go并发编程学习的简单代码示例：生产者消费者模型"><img class="cover" src= "" data-lazy-src="/img/cover9.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">Go并发编程学习的简单代码示例：生产者消费者模型</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/2023/04/17/Go%E7%BD%91%E7%BB%9C%E7%BC%96%E7%A8%8B%E7%9A%84%E5%AD%A6%E4%B9%A0%E4%BB%A3%E7%A0%81%E7%A4%BA%E4%BE%8B%EF%BC%9A%E5%AE%A2%E6%88%B7%E7%AB%AF_%E6%9C%8D%E5%8A%A1%E7%AB%AF%EF%BC%88C_S%EF%BC%89%E6%A8%A1%E5%9E%8B/" title="Go网络编程的学习代码示例：客户端&#x2F;服务端（C&#x2F;S）模型"><img class="cover" src= "" data-lazy-src="/img/cover3.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-04-17</div><div class="title">Go网络编程的学习代码示例：客户端&#x2F;服务端（C&#x2F;S）模型</div></div></a></div><div><a href="/2023/04/16/Go%E5%B9%B6%E5%8F%91%E7%BC%96%E7%A8%8B%E7%9A%84%E5%AD%A6%E4%B9%A0%E4%BB%A3%E7%A0%81%E7%A4%BA%E4%BE%8B%EF%BC%9A%E7%94%9F%E4%BA%A7%E8%80%85%E6%B6%88%E8%B4%B9%E8%80%85%E6%A8%A1%E5%9E%8B/" title="Go并发编程学习的简单代码示例：生产者消费者模型"><img class="cover" src= "" data-lazy-src="/img/cover9.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-04-16</div><div class="title">Go并发编程学习的简单代码示例：生产者消费者模型</div></div></a></div><div><a href="/2023/04/18/Go%E7%BD%91%E7%BB%9C%E7%BC%96%E7%A8%8B%E7%9A%84%E5%AD%A6%E4%B9%A0%E4%BB%A3%E7%A0%81%E7%A4%BA%E4%BE%8B%EF%BC%9A%E7%BD%91%E7%BB%9C%E6%96%87%E4%BB%B6%E4%BC%A0%E8%BE%93/" title="Go网络编程的学习代码示例：网络文件传输"><img class="cover" src= "" data-lazy-src="/img/cover10.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-04-18</div><div class="title">Go网络编程的学习代码示例：网络文件传输</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content is-expand"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%89%8D%E8%A8%80"><span class="toc-number">1.</span> <span class="toc-text">前言</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%B8%80%E3%80%81%E9%97%AE%E9%A2%98"><span class="toc-number">2.</span> <span class="toc-text">一、问题</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%BA%8C%E3%80%81%E6%B5%8B%E8%AF%95%E7%8E%AF%E5%A2%83"><span class="toc-number">3.</span> <span class="toc-text">二、测试环境</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%B8%89%E3%80%81%E5%90%8C%E6%A8%A1%E5%9D%97%E5%90%8C%E7%9B%AE%E5%BD%95%E5%90%8C%E5%8C%85"><span class="toc-number">4.</span> <span class="toc-text">三、同模块同目录同包</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#1-%E7%9B%AE%E5%BD%95%E7%BB%93%E6%9E%84"><span class="toc-number">4.1.</span> <span class="toc-text">1.目录结构</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-%E6%93%8D%E4%BD%9C"><span class="toc-number">4.2.</span> <span class="toc-text">2.操作</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-%E6%80%BB%E7%BB%93"><span class="toc-number">4.3.</span> <span class="toc-text">3.总结</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%9B%9B%E3%80%81%E5%90%8C%E6%A8%A1%E5%9D%97%E4%B8%8D%E5%90%8C%E7%9B%AE%E5%BD%95%E4%B8%8D%E5%90%8C%E5%8C%85"><span class="toc-number">5.</span> <span class="toc-text">四、同模块不同目录不同包</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#1-%E7%9B%AE%E5%BD%95%E7%BB%93%E6%9E%84-1"><span class="toc-number">5.1.</span> <span class="toc-text">1.目录结构</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-%E6%93%8D%E4%BD%9C-1"><span class="toc-number">5.2.</span> <span class="toc-text">2.操作</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-%E6%80%BB%E7%BB%93-1"><span class="toc-number">5.3.</span> <span class="toc-text">3.总结</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%BA%94%E3%80%81%E4%B8%8D%E5%90%8C%E6%A8%A1%E5%9D%97%E4%B8%8D%E5%90%8C%E7%9B%AE%E5%BD%95%E4%B8%8D%E5%90%8C%E5%8C%851"><span class="toc-number">6.</span> <span class="toc-text">五、不同模块不同目录不同包1</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#1-%E7%9B%AE%E5%BD%95%E7%BB%93%E6%9E%84-2"><span class="toc-number">6.1.</span> <span class="toc-text">1.目录结构</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-%E6%93%8D%E4%BD%9C%EF%BC%9A"><span class="toc-number">6.2.</span> <span class="toc-text">2.操作：</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%EF%BC%885%EF%BC%89%E5%91%BD%E4%BB%A4%E8%A1%8C%E6%89%A7%E8%A1%8C%EF%BC%9A"><span class="toc-number">6.3.</span> <span class="toc-text">（5）命令行执行：1234567cd dir# 方式一go run .# 方式二go run a.go</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-%E6%80%BB%E7%BB%93-2"><span class="toc-number">6.4.</span> <span class="toc-text">3.总结</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%85%AD%E3%80%81%E4%B8%8D%E5%90%8C%E6%A8%A1%E5%9D%97%E4%B8%8D%E5%90%8C%E7%9B%AE%E5%BD%95%E4%B8%8D%E5%90%8C%E5%8C%852"><span class="toc-number">7.</span> <span class="toc-text">六、不同模块不同目录不同包2</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#1-%E7%9B%AE%E5%BD%95%E7%BB%93%E6%9E%84-3"><span class="toc-number">7.1.</span> <span class="toc-text">1.目录结构</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-%E6%93%8D%E4%BD%9C%EF%BC%9A-1"><span class="toc-number">7.2.</span> <span class="toc-text">2.操作：</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%EF%BC%885%EF%BC%89%E5%91%BD%E4%BB%A4%E8%A1%8C%E6%89%A7%E8%A1%8C%EF%BC%9A-1"><span class="toc-number">7.3.</span> <span class="toc-text">（5）命令行执行：1234567cd dir_a# 方式一go run .# 方式二go run a.go</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-%E6%80%BB%E7%BB%93-3"><span class="toc-number">7.4.</span> <span class="toc-text">3.总结</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%B8%83%E3%80%81%E6%80%BB%E7%BB%93"><span class="toc-number">8.</span> <span class="toc-text">七、总结</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E6%80%BB%E7%BB%93"><span class="toc-number">9.</span> <span class="toc-text">总结</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%8F%82%E8%80%83%E8%B5%84%E6%96%99"><span class="toc-number">10.</span> <span class="toc-text">参考资料</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%BD%9C%E8%80%85%E7%9A%84%E8%AF%9D"><span class="toc-number">11.</span> <span class="toc-text">作者的话</span></a></li></ol></div></div></div></div></main><footer id="footer" style="background-image: url('/img/cover16.png')"><div id="footer-wrap"><div class="copyright">&copy;2023 - 2025 By 夜悊</div><div class="footer_custom_text"><a target="_blank" href="https://beian.miit.gov.cn/" >琼ICP备2023001225号-1</a><br><img src= "" data-lazy-src="/img/beian.png"/>&nbsp<a target="_blank" href="https://www.beian.gov.cn/portal/registerSystemInfo?recordcode=46010802001221" >琼公网安备 46010802001221号</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="translateLink" type="button" title="简繁转换">繁</button><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button></div><div id="rightside-config-show"><button id="rightside-config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js?v=4.13.0"></script><script src="/js/main.js?v=4.13.0"></script><script src="/js/tw_cn.js?v=4.13.0"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui@5.0.33/dist/fancybox/fancybox.umd.min.js"></script><script src="https://cdn.jsdelivr.net/npm/vanilla-lazyload@17.8.8/dist/lazyload.iife.min.js"></script><div class="js-pjax"></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div><div id="local-search-stats-wrap"></div></div></div><div id="search-mask"></div><script src="/js/search/local-search.js?v=4.13.0"></script></div></div></body></html>